package cn.tyf.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.tyf.domain.User;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * 拦截器，登录检查
 */

public class LoginInterceptor implements HandlerInterceptor {

    /**
     * 在请求被处理之前调用
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        if (request.getHeader("wxcookie")!= null || request.getRequestURI().contains("register")) {
            return true;
        }
        if (request.getRequestURI().contains("setMinValue")) {
            return true;
        }
        if (request.getRequestURI().contains("userLogin") || request.getRequestURI().contains("adminLogin")) {
            return true;
        }
        if (request.getRequestURI().contains(".css") || request.getRequestURI().contains(".js")
                || request.getRequestURI().contains(".jpg")|| request.getRequestURI().contains(".png")) {
            return true;
        }
        if (request.getRequestURI().contains("code")|| request.getRequestURI().contains("findOpenid")
                || request.getRequestURI().contains("delOpenId")) {
            return true;
        }
        if (request.getRequestURI().contains("cookieLogin") || request.getRequestURI().contains("sessionLogin")){
            return true;
        }
        Object userLoign = request.getSession().getAttribute("userLogin");
        if (userLoign != null) {
            return true;
        }
        response.getWriter().print("index.jsp");
        return false;
    }

}